package com.cmsky.open.food.manager;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.cmsky.open.base.model.AreaInfo;
import com.cmsky.open.core.base.*;
import com.cmsky.open.core.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.cmsky.open.food.model.*;
import com.cmsky.open.food.dao.*;
import com.cmsky.open.food.manager.*;
import com.cmsky.open.food.vo.query.*;

/**
 * @author jandyfish email:jandyfish(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


@Service
@Transactional
public class FoodAreaInfoManager extends BaseManager<FoodAreaInfo,java.lang.Integer>{

	private FoodAreaInfoDao foodAreaInfoDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setFoodAreaInfoDao(FoodAreaInfoDao dao) {
		this.foodAreaInfoDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.foodAreaInfoDao;
	}
	
	public synchronized int saveOrUpdate(FoodAreaInfo entity) {
		if(entity.getId() == null 
				|| entity.getId() <= 0) 
			return save(entity);
		else 
			return update(entity);
	}
	
	@Transactional(readOnly=true)
	public BasePager findPage(FoodAreaInfoQuery query) {
		return foodAreaInfoDao.findPage(query);
	}
	
	public List<AreaInfo> findAreaInfosByFoodid(Integer foodId){
		return foodAreaInfoDao.findAreaInfosByFoodid(foodId);
	}
	/**
	 * 先删除关联关系，再进行新关系的匹配
	 * @param infoList
	 * @return
	 */
	public synchronized int saveOrUpdate(List<FoodAreaInfo> infoList,Integer foodId){
		int count = 0;
		FoodAreaInfoQuery query = new FoodAreaInfoQuery();
		query.setFoodId(foodId);
		this.foodAreaInfoDao.removeByKey(query);
		for(FoodAreaInfo bean : infoList){
			if(bean.getFoodId().equals(foodId)){
				if(this.save(bean) > 0){
					count += 1;
				}
			}
		}
		return count;
	}
}
